<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 140px;
            line-height: 50px;
            background-color: #f00;
            text-align: center;
        }
    </style>
</head>

<body>

    <div>获取验证码</div>

    <script>

        // 定时器多次点击会产生叠加的问题

        var oDiv = document.querySelector('div');

        // 开关  ---  记录是否有定时器在执行
        var flag = true;


        // 绑定事件
        oDiv.onclick = function () {

            if (flag) {
                flag = false;

                // 计数器
                var count = 10;
                oDiv.innerHTML = count;
                oDiv.style.background = '#ddd';
                // 倒计时
                var t = setInterval(function () {
                    count--;
                    oDiv.innerHTML = count
                    if (count === 0) {
                        clearInterval(t);
                        flag = true;
                        oDiv.innerHTML = '获取验证码';
                        oDiv.style.background = '#f00';
                    }
                }, 1000)
            }


        }


    </script>

</body>

</html>